<?php
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/admindetails.php';
include_once '../../model/email.php';
include_once("../../model/users.php");
require_once '../../model/state.php';
require_once '../../redirect.php';
## Create Objects
/*******************************/
$adminObj = new Model_AdminDetails();
$emailObj = new  Model_Email();
$userObj = new Model_Users();
$stateObj = new Model_State();
/*******************************/

checkUserLogin();

if($_SESSION['user_type'] == '3')
{
	@header("location:".SITE_URL."/");
	exit;
}


$userId = $_SESSION['getbids_id'];
$userDetArray = $userObj->getUserDetailsByUserId($userId);
$smarty->assign('userDetArray',$userDetArray);

$PhotoID = base64_decode($_GET['id1']);
if($PhotoID!='')
{
	$userPhotoDetArray = $userObj->getUserPhotoDetailsByPhotoId($PhotoID);
	$smarty->assign('userPhotoDetArray',$userPhotoDetArray);
}

$getpostalcodedetail = $stateObj->getPostalcodeExits($userDetArray['workarea']);
$smarty->assign('getpostalcodedetail',$getpostalcodedetail);

if(isset($_POST['title']) && isset($_FILES['image']['name'])) 
{
	extract($_POST);
	
			$userArray = array();			
			$PhotoID = $_POST["photo_id"];	 
			$userArray["user_id"] = $userId;			
			$userArray["title"] = $_POST["title"];			
			$err = false;
			if($_FILES['image']['name'] != '')
			{
				$size =	getimagesize($_FILES['image']['tmp_name']);
				if($size[0] >= WORKIMAGEWIDTH && $size[1] >= WORKIMAGEHEIGHT) 
				{   
					if($PhotoID!='')
					{ 
						$userPhotoDetArray = $userObj->getUserPhotoDetailsByPhotoId($PhotoID);	
						@unlink('../../uploads/tradesmanjobs/'.$userPhotoDetArray['image']);
					}


					$path = '../../uploads/tradesmanjobs/'; 
					$imageName    = $_FILES['image']['name']; 
	
					$arrImageName = explode(".",$imageName);
					$position     = count($arrImageName);
					
					$fileName     = date('Ymdhis').".".$arrImageName[$position-1]; 
					@copy($_FILES['image']['tmp_name'], $path.$fileName);
					
					$userArray['image']	= $fileName;
	
				}
				else
				{
				   $err = true;				   
				}
			}
			if($err == false)
			{
				if($PhotoID!='')
				{ 
					$userObj->editUserWorkPhotoByPhotoID($userArray,$PhotoID);			
					$_SESSION['msg'] = "<div class='success_msg'><span>Worked photo edited successfully.</span></div>";
					redirect_to_link(SITE_URL.'/myaccount/myuploadedphoto/');
				}
				else
				{
					$userArray["date_added"] = date("Y-m-d");											
					$userObj->addUserWorkPhotoByValue($userArray);			
					$_SESSION['msg'] = "<div class='success_msg'><span>Worked photo added successfully.</span></div>";
					redirect_to_link(SITE_URL.'/myaccount/myuploadedphoto/');
				}				
				exit;
			}
			else
			{
					$_SESSION['msg'] = "<div class='error_msg'><span>".WORKIMAGE_WIDTHHEIGHT."</span></div>";
			}
		
	
}


if($_SESSION['msg'] != ''){
	$smarty->assign("msg", $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

$active = "myaccount";
$smarty->assign("active", $active);
$subactive ="myuploadedphoto";
$smarty->assign("subactive", $subactive);
$smarty->assign('mainmenu', '1');
$smarty->display(TEMPLATEDIR . '/modules/myaccount/adduploadimage.tpl');
?>